<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="创意词包" :visible.sync="dialogVisible" width="500px">
        <el-table :data="tableData" class="com_table" height="400px">
            <el-table-column prop="name" label="词包">
            </el-table-column>
            <el-table-column prop="default_word" label="默认词">
            </el-table-column>
            <el-table-column prop="" label="用户覆盖率">
            </el-table-column>
            <el-table-column prop="name" label="操作">
                <template slot-scope="scope">
                    <el-link type="primary" :underline="false" @click="addWrod(scope.row)">插入</el-link>
                </template>
            </el-table-column>
        </el-table>
    </el-dialog>
</template>

<script>
export default {
    props: ["keywords"],
    name: "",
    data() {
        return {
            dialogVisible: false,
            wordLength: [],
            index: null,
            type: null,
            tableData: [],
        };
    },
    created() {
        this.$bus.on("addMoreWord", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.wordLength = value.title;
            this.index = value.index;
            this.type = value.type;
            this.tableData = this.keywords;
        });
    },
    methods: {
        addWrod(word) {
            if (this.type == 1) {
                if (this.wordLength.length >= 2) {
                    this.$message.error("最多只能插入2个创意包");
                    return;
                } else {
                    let text = this.$parent.createform.creatinfo.creative[this.index].title
                    text = this.$parent.createform.creatinfo.creative[this.index].title.slice(0, this.$parent.blurIndex) + "{" + word.name + "}"  + this.$parent.createform.creatinfo.creative[this.index].title.slice(this.$parent.blurIndex);
                    this.$parent.createform.creatinfo.creative[this.index].title = text
                    // this.$parent.createform.creatinfo.creative[
                    //     this.index
                    // ].title += "{" + word.name + "}";
                    this.$parent.createform.creatinfo.creative[
                        this.index
                    ].dynamic_words.push({
                        word_id: word.creative_word_id,
                        dict_name: word.name,
                        default_word: word.default_word,
                    });
                    this.dialogVisible = false;
                }
            } else if (this.type == 2) {
                if (this.wordLength.length >= 2) {
                    this.$message.error("最多只能插入2个创意包");
                    return;
                } else {
                    
                    let text = this.$parent.createform.creattitle.title[this.index].title
                    text = this.$parent.createform.creattitle.title[this.index].title.slice(0, this.$parent.blurIndex) + "{" + word.name + "}"  + this.$parent.createform.creattitle.title[this.index].title.slice(this.$parent.blurIndex);
                    this.$parent.createform.creattitle.title[this.index].title = text
                    // this.$parent.createform.creattitle.title[
                    //     this.index
                    // ].title += "{" + word.name + "}";
                    this.$parent.createform.creattitle.title[
                        this.index
                    ].dynamic_words.push({
                        word_id: word.creative_word_id,
                        dict_name: word.name,
                        default_word: word.default_word,
                    });
                    this.dialogVisible = false;
                }
            }
            this.$parent.sessionSave();
        },
    },
    components: {},
};
</script>


<style scoped>
</style>
